﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;

namespace ImageCompression
{
    /// <summary>
    /// 图片压缩小工具
    /// Bao
    /// 联系：gsbhz@qq.com
    /// </summary>
    public partial class MainForm : Form
    {
        #region MainForm
        public MainForm()
        {
            InitializeComponent();

            init();
        }
        #endregion

        #region 初始化
        private void init()
        {
            this.cmbRate.SelectedItem = "80";

            this.gvList.AutoGenerateColumns = false;
        }
        #endregion

        #region 事件

        #region 选择文件夹
        private void btnSelectSavePath_Click(object sender, EventArgs e)
        {
            this.txtSavePath.Text = Common.SelectFolder();
        }
        #endregion

        // 工具栏事件
        #region 选择单个图片
        private void tbtnAddImg_Click(object sender, EventArgs e)
        {
            //this.gvList.DataSource = Common.Array2List(Common.SelectFiles());
            this.gvList.Append(this.lblProgress, Common.Array2List(Common.SelectFiles()));
        }
        #endregion

        #region 选择文件夹
        private void tbtnAddFolder_Click(object sender, EventArgs e)
        {
            string path = Common.SelectFolder();
            //this.gvList.DataSource = Common.GetAllImg(path);
            this.gvList.Append(this.lblProgress, Common.GetAllImg(path));
        }
        #endregion

        #region 清除选中的文件
        private void tbtnClearItem_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow r in this.gvList.SelectedRows)
            {
                this.gvList.Rows.Remove(r);
            }
            this.lblProgress.Text = "0/" + this.gvList.Rows.Count;
        }
        #endregion

        #region 清空所有文件
        private void tbtnClearAll_Click(object sender, EventArgs e)
        {
            this.gvList.Rows.Clear();
            this.lblProgress.Text = "0/" + this.gvList.Rows.Count;
        }
        #endregion

        #region 开始压缩
        private void tbtnRun_Click(object sender, EventArgs e)
        {
            compression();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            compression();
        }
        #endregion

        #region 关于
        private void tbtnAbout_Click(object sender, EventArgs e)
        {

        }
        #endregion

        #endregion

        // =================公共方法=================
        #region 公共方法

        // 压缩
        private void compression()
        {
            string path = this.txtSavePath.Text + "/" + this.cmbRate.Text;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            toolStripStatusLabel1.Text = "保存路径:" + path;

            ImgCompression c = new ImgCompression(this.gvList.ToList(), this.cmbRate.Text.ToInt(), path, txtpost.Text, chkOri.Checked, 100,chkCover_backup.Checked);
            c.ProgressBar = this.progressBar1;
            c.ProgressText = this.lblProgress;
            c.Dgv = this.gvList;
            c.Start();
        }

        // 按尺寸压缩
        private void compression2(int width, int height, bool isAcorss, bool isVertical)
        {
            string path = this.txtSavePath.Text + "/" + width + "_" + height;

            //if (!Directory.Exists(path))
            //{
            //    Directory.CreateDirectory(path);
            //}

            //toolStripStatusLabel1.Text = "保存路径:" + path;

            ImgCompression2 c = new ImgCompression2(this.gvList.ToList(), width, height, path, isAcorss, isVertical, txtpost.Text);
            c.ProgressBar = this.progressBar1;
            c.ProgressText = this.lblProgress;
            c.Dgv = this.gvList;
            c.Start();
        }

        #endregion

        private void btnClearList_Click(object sender, EventArgs e)
        {
            this.gvList.Rows.Clear();
        }

        #region 按像素等比压缩
        private void txtwidth_TextChanged(object sender, EventArgs e)
        {
            int i = 0;
            if(!int.TryParse(txtwidth.Text,out i))
            {
                txtwidth.Text = "";
            }

            if(txtwidth.TextLength>0)
            {
                txtheight.Text = "";
            }
        }

        private void txtheight_TextChanged(object sender, EventArgs e)
        {
            int i = 0;
            if (!int.TryParse(txtheight.Text, out i))
            {
                txtheight.Text = "";
            }

            if (txtheight.TextLength > 0)
            {
                txtwidth.Text = "";
            }
        }

        private void btnCompress_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtwidth.Text) && string.IsNullOrEmpty(txtheight.Text))
            {
                MessageBox.Show("输入宽或高");
                return;
            }

            if (string.IsNullOrEmpty(txtwidth.Text))
            {
                compression2(0, int.Parse(txtheight.Text),chkAcross.Checked,chkVertical.Checked);
            }
            else
            {
                compression2(int.Parse(txtwidth.Text), 0, chkAcross.Checked, chkVertical.Checked);
            }
        }

        #endregion

        #region 按指定尺寸压缩
        private void txtwidth2_TextChanged(object sender, EventArgs e)
        {
            int i = 0;
            if (!int.TryParse(txtwidth2.Text, out i))
            {
                txtwidth2.Text = "";
            }
        }

        private void txtheight2_TextChanged(object sender, EventArgs e)
        {
            int i = 0;
            if (!int.TryParse(txtheight2.Text, out i))
            {
                txtheight2.Text = "";
            }
        }

        private void btnCompress2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtwidth2.Text) || string.IsNullOrEmpty(txtheight2.Text))
            {
                MessageBox.Show("输入宽和高");
                return;
            }

            compression2(int.Parse(txtwidth2.Text), int.Parse(txtheight2.Text), chkA2.Checked, chkV2.Checked);
        }

        #endregion


        /// <summary>
        /// 图片质量压缩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnzl_Click(object sender, EventArgs e)
        {
            string path = this.txtSavePath.Text + "/" + this.cbxzl.Text;

            //if (!Directory.Exists(path))
            //{
            //    Directory.CreateDirectory(path);
            //}

            //toolStripStatusLabel1.Text = "保存路径:" + path;

            ImgCompression c = new ImgCompression(this.gvList.ToList(), 100, path, txtpost.Text, chkOri.Checked, int.Parse(this.cbxzl.Text), chkCover_backup.Checked);
            c.ProgressBar = this.progressBar1;
            c.ProgressText = this.lblProgress;
            c.Dgv = this.gvList;
            c.Start();
        }

        /// <summary>
        /// 清空输出目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClearOut_Click(object sender, EventArgs e)
        {
            if (this.chkOri.Checked||this.chkCover_backup.Checked)
            {
                foreach (ImgInfoEntity img in this.gvList.ToList())
                {
                    DeleteFolder(Path.GetDirectoryName(img.FullPath)+"/out");

                }
            }
            else
            {
                string path = this.txtSavePath.Text;
                DeleteFolder(path);
            }

            MessageBox.Show("完成");
        }

        /// <summary>
        /// 递归删除文件夹目录及文件
        /// </summary>
        /// <param name="dir"></param>  
        /// <returns></returns>
        public static void DeleteFolder(string dir)
        {
            try
            {
                if (Directory.Exists(dir)) //如果存在这个文件夹删除之 
                {
                    foreach (string d in Directory.GetFileSystemEntries(dir))
                    {
                        if (File.Exists(d))
                            File.Delete(d); //直接删除其中的文件                        
                        else
                            DeleteFolder(d); //递归删除子文件夹 
                    }
                    Directory.Delete(dir, true); //删除已空文件夹                 
                }
            }
            catch { }
        }

        private void chkOri_CheckedChanged(object sender, EventArgs e)
        {
            if(chkCover_backup.Checked)
            {
                chkCover_backup.Checked = false;
            }
        }

        private void chkCover_backup_CheckedChanged(object sender, EventArgs e)
        {
            if (chkOri.Checked)
            {
                chkOri.Checked = false;
            }
        }
    }
}
